package yxy.game.pm2.active.command.paylevelpackage;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actgraderecharge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.paylevelpackage.ActiveModule_支付等级礼包;
import yxy.game.pm2.active.module.paylevelpackage.PLPInfo;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActPayLevelPackageInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

@MessageType(proto = Command.ActPayLevelPackageInfo, classtype = ActiveMessage.ActPayLevelPackageInfoRequest.class)
public class CMD_Active_支付等级礼包_Info extends Command<ActPayLevelPackageInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_支付等级礼包_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActPayLevelPackageInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActPayLevelPackageInfoResponse.Builder response = ActiveMessage.ActPayLevelPackageInfoResponse.newBuilder();
		
		ActiveModule_支付等级礼包 am = ActiveModule_支付等级礼包.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		Map<String, actgraderecharge> execlmap = ActDataCenter.getAllData(actgraderecharge.class, playercode);
		
		for(PLPInfo plp : am.getPlps().values()) {
			if(!plp.isOpen()) {
				continue;
			}
			
			actgraderecharge execl = execlmap.get(String.valueOf(plp.getId()));
			
			ActiveMessage.PLPackage.Builder build = ActiveMessage.PLPackage.newBuilder();
			
			build.setId(plp.getId());
			build.setGoodsid(plp.getGoodsid());
			build.setLevel(execl.getCondition());
			build.setEndtime(plp.getEndtime());
			build.setRebate(execl.getRebate()); //返利倍数
			
			Collection<Currency> curs = Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
//			int canget = plp.getBuytime() != null ? ActiveModule_支付等级礼包.hadget : plp.getLock() == null ? ActiveModule_支付等级礼包.canget :
//				ActiveModule_支付等级礼包.cannotget;
			int canget = plp.getBuytime() != null ? DrawStatus.hadget.getStatus() : plp.getLock() == null ? DrawStatus.canget.getStatus() :
				DrawStatus.cannotget.getStatus();
			build.setCanget(canget);
			
			response.addPlpackages(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActPayLevelPackageInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
